#ifndef DATATREE_H
#define DATATREE_H

#include <QObject>
#include <QStringList>
#include <QScriptValue>
#include <QScriptContext>

QScriptValue dataTreeConstructor(QScriptContext *context, QScriptEngine *engine);

class DataTreeHelper : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QStringList fields READ getFields WRITE setFields)
    Q_PROPERTY(QString filter READ getFilter WRITE setFilter)
    Q_PROPERTY(QString tableName READ getTableName WRITE setTableName)
    Q_PROPERTY(QString databaseName READ getDatabaseName WRITE setDatabaseName)
    Q_PROPERTY(QStringList ids READ getIdentificators WRITE setIdentificators)

public:
    explicit DataTreeHelper(const QString &tableName = QString(), const QString fields = QString());

    void setDatabaseName( const QString &dbName ) { m_databaseName = dbName; }
    QString getDatabaseName() { return m_databaseName; }

    void setTableName( const QString &tbName ) { m_tableName = tbName; }
    QString getTableName() { return m_tableName; }

    void setFilter( const QString &filter ) { m_filter = filter; }
    QString getFilter() { return m_filter; }

    void setFields( const QStringList &fields ) { m_fields = fields; }
    Q_INVOKABLE void setFields(const QString &fieldsBySemicolon) { m_fields = fieldsBySemicolon.split(';'); }
    QStringList getFields() { return m_fields; }

    void setIdentificators(const QStringList &ids) { m_ids =ids; }
    QStringList getIdentificators() { return m_ids; }

    QStringList tablesStringList();
    QList<DataTreeHelper*> dataTreeList();

    DataTreeHelper* childById(int index) { return m_childs.at(index); }
    Q_INVOKABLE void appendChild( DataTreeHelper* child);
    Q_INVOKABLE int childsCount();
    Q_INVOKABLE void removeChild(int index);
    Q_INVOKABLE void debug();

private:
    QStringList tablesStringList( QStringList &list);
    QList<DataTreeHelper*> dataTreeList( QList<DataTreeHelper*> &list);

private:
    QString m_databaseName;
    QString m_tableName;
    QString m_filter;
    QStringList m_fields;
    QList<DataTreeHelper*> m_childs;
    QStringList m_ids;
};

#endif // DATATREE_H
